package com.jokins.common.actions.exception;

import com.jokins.common.util.BusinessCode;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2024/9/9
 */
@NoArgsConstructor
@AllArgsConstructor
public class BusinessException extends RuntimeException{

    private Long businessCode;

    private String businessType;

    private String message;

    private HttpStatus httpStatus;

    public BusinessException(BusinessCode businessCode) {
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
    }

    public BusinessException(String message, final BusinessCode businessCode) {
        super(message);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();    }

    public BusinessException(final String message, final BusinessCode businessCode, final HttpStatus httpStatus) {
        super(message);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
        this.httpStatus = httpStatus;
    }

    /**
     *
     * DUBBO使用FastJSON来反序列化异常时,
     * 会检查调用创建异常会检查这个构造方法来注入message内容,不然message内容会丢失
     * 可以查看fastJSON源码文件ThrowableDeserializer的createException方法看到处理方式
     */

    public BusinessException(final String message, final Throwable cause) {
        super(message, cause);
        this.message = message;
    }

    public BusinessException(final String message, final BusinessCode businessCode, final Throwable cause) {
        super(message, cause);
        this.message = message;
        this.businessCode = businessCode.getCode();
        this.businessType = businessCode.name();
    }

    /**
     * @return the message
     */
    @Override
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(final String message) {
        this.message = message;
    }

    /**
     * @return the businessCode
     */
    public Long getBusinessCode() {
        return businessCode;
    }

    public void setBusinessCode(Long businessCode) {
        this.businessCode = businessCode;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public String getBusinessType() {
        return businessType;
    }

    public void setBusinessType(String businessType) {
        this.businessType = businessType;
    }
}